//
// Created by Yaotian on 2/7/14.
// Copyright (c) 2014 Kollway Mobile. All rights reserved.
//

#import "KWMImageUtil.h"


@implementation KWMImageUtil {
    
}

+ (UIImage *)createTransparentImage {
    UIImage *result = nil;
    
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
    result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return result;
}

+ (UIImage *)createColorImage:(UIColor *)color {
    UIImage *result = nil;
    
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
    [color setFill];
    UIRectFill(rect); // Fill it with your color
    result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return result;
}

+ (UIImage *)createGradientImage:(UIColor *)startColor endColor:(UIColor *)endColor size:(CGSize)size {
    UIImage *result = nil;
    
    CGPoint center = CGPointMake(size.width/2, size.height/2);
    
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    
    CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
    
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat r0;
    CGFloat g0;
    CGFloat b0;
    CGFloat a0;
    CGFloat r1;
    CGFloat g1;
    CGFloat b1;
    CGFloat a1;
    [startColor getRed:&r0 green:&g0 blue:&b0 alpha:&a0];
    [endColor getRed:&r1 green:&g1 blue:&b1 alpha:&a1];
    CGFloat components[8] = {
        r0, g0, b0, a0,   // Start color
        r1, g1, b1, a1 }; // End color
    CGGradientRef gradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
    CGContextDrawLinearGradient(currentContext, gradient, CGPointMake(0, 0), CGPointMake(0, size.height), 0);
    
    result = UIGraphicsGetImageFromCurrentImageContext();
    CGGradientRelease(gradient);
    CGColorSpaceRelease(rgbColorspace);
    
    return result;
}

+ (BOOL)saveJPGImageToFile:(NSString *)filePath image:(UIImage *)image {
    NSData *data = UIImageJPEGRepresentation(image, 1.0f);
    return [data writeToFile:filePath atomically:YES];
}

+ (BOOL)savePNGImageToFile:(NSString *)filePath image:(UIImage *)image {
    NSData *data = UIImagePNGRepresentation(image);
    return [data writeToFile:filePath atomically:YES];
}

+ (UIImage *)getImageFromFile:(NSString *)filePath {
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    return [UIImage imageWithData:data];
}

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        if ([[UIScreen mainScreen] scale] == 2.0) {
            UIGraphicsBeginImageContextWithOptions(newSize, YES, 2.0);
        } else {
            UIGraphicsBeginImageContext(newSize);
        }
    } else {
        UIGraphicsBeginImageContext(newSize);
    }
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

+ (UIImage *)imageAfterZoom:(UIImage *)image scale:(float)scale {
    float newWidth = image.size.width * scale;
    float newHeight = image.size.height * scale;
    CGSizeMake(newWidth, newHeight);
    return [self imageWithImage:image scaledToSize:CGSizeMake(newWidth, newHeight)];
}


@end